package cfg

import (
	"sync"

	"github.com/BurntSushi/toml"
)

var (
	instance *config
	once     sync.Once
)

//获取配置文档实例
func Instance() *config {
	once.Do(func() {
		var conf config
		filePath := "./config/config.toml"
		if _, err := toml.DecodeFile(filePath, &conf); err != nil {
			return
		}
		instance = &conf
	})

	return instance
}

type config struct {
	Status   status
	Admin    admin
	Api      api
	Task     task
	Database database
	Logger   logger
	Gen      gen
	AppConf  appconfig
}

type status struct {
	Admin bool
	Api   bool
}

type admin struct {
	Address    string
	ServerRoot string
	Swagger    string
}

type jwt struct {
	Timeout    int
	Refresh    int
	EncryptKey string
}

type api struct {
	Address    string
	ServerRoot string
	Jwt        jwt
}

type task struct {
	WorkPoolSize int
}

type database struct {
	Master string
	Slave  string
	Debug  bool
	Log    string
}

type logger struct {
	Path   string
	Level  uint32
	Stdout bool
}

type gen struct {
	Author        string
	ModuleName    string
	PackageName   string
	AutoRemovePre bool
	TablePrefix   string
}

type appconfig struct {
	BaseUrl   string
	SiteTitle string

	// Github
	Github struct {
		ClientID     string
		ClientSecret string
	}

	// QQ登录
	QQConnect struct {
		AppId  string
		AppKey string
	}

	// 阿里云oss配置
	Uploader struct {
		Enable    string
		AliyunOss struct {
			Host          string
			Bucket        string
			Endpoint      string
			AccessId      string
			AccessSecret  string
			StyleSplitter string
			StyleAvatar   string
			StylePreview  string
			StyleSmall    string
			StyleDetail   string
		}

		Local struct {
			Host string
			Path string
		}
	}

	// 百度ai
	BaiduAi struct {
		ApiKey    string
		SecretKey string
	}

	// 百度SEO相关配置
	// 文档：https://ziyuan.baidu.com/college/courseinfo?id=267&page=2#h2_article_title14
	BaiduSEO struct {
		Site  string
		Token string
	}

	// 神马搜索SEO相关
	// 文档：https://zhanzhang.sm.cn/open/mip
	SmSEO struct {
		Site     string
		UserName string
		Token    string
	}

	// smtp
	Smtp struct {
		Host     string
		Port     string
		Username string
		Password string
		SSL      bool
	}

	// es
	Es struct {
		Url   string
		Index string
	}
}
